<?php

namespace app\admin\service;

use app\model\Company;

class CompanyService
{
    private $companyModel;

    public function __construct(Company $companyModel)
    {
        $this->companyModel = $companyModel;
    }

    // 添加/编辑公司
    public function editCompany($params)
    {
        if (empty($params['id'])) {
            $params['created_at'] = time();
        } else {
            $params['updated_at'] = time();
        }
        return $this->companyModel->edit($params);
    }

    // 获取公司列表
    public function getCompanyList($params)
    {
        $where = [];
        $page = $params['page'] ?? 1;
        $limit = $params['limit'] ?? 10;

        if (!empty($params['name'])) {
            $where[] = ['name', 'like', '%' . $params['name'] . '%'];
        }

        $data = $this->companyModel->getPagingDataByWhere($where, $page, $limit, 'id, name, contacts, contact_number, is_distributor, payment_method, payee, collection_account, bank_name, tax_rate', 'id desc');
        return $data;
    }

    // 获取下拉数据
    public function getSelectList($params)
    {
        $where = [];
        if (!empty($params['type'])) {
            $where = ['is_distributor' => 0];
        }
        return $this->companyModel->getDataByWhere($where);
    }

    // 公司详情
    public function getCompanyDetail($id)
    {
        return $this->companyModel->getInfoById($id, 'id, name, contacts, contact_number, is_distributor, payment_method, payee, collection_account, bank_name, tax_rate');
    }
}